package com.huonan.utils;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
* @Description TODO
* @Author huonan
* @Date 2021/8/26 上午11:52
* @Version V1.0
**/
public final class ExecutorUtil {
    public static ExecutorService getExecutorService(int size) {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("demo-pool-%d").build();

        //Common Thread Pool
        return new ThreadPoolExecutor(size, size,
                10L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(2000), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
    }

    public static void closePool(ExecutorService threadPool) {
        threadPool.shutdown();
        if (threadPool.isTerminated()) {
            Thread.yield();
        }
    }
}
